package com.common.basis.util;

import com.common.basis.entity.CalendarAO;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 日历工具类
 *
 * @author guochaowang
 * @date 2022/06/17
 */
public class CalendarUtil {
    /**
     * 获取指定日期的上一个月的第一天
     */
    public static List<CalendarAO> getCalendarByMonth(int year, int month) {
        ArrayList<CalendarAO> list = new ArrayList<>();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MINUTE, month);
        cal.set(Calendar.DATE, 1);
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);
        for (int i = 0; i < dayOfWeek; i++) {
            list.add(CalendarAO.builder()
                    .day(0)
                    .build());
        }
        int actualMaximum = cal.getActualMaximum(Calendar.DATE);
        if (year == 1582 && month == 10) {
            for (int i = 1; i <= actualMaximum; i++) {
                if (!(i > 4 && i < 15)) {
                    list.add(CalendarAO.builder()
                            .day(i)
                            .build());
                }
            }
        } else {
            for (int i = 1; i <= actualMaximum; i++) {
                list.add(CalendarAO.builder()
                        .day(i)
                        .build());
            }
        }

        return list;
    }
}
